package com.yomahub.liteflow.example.threadproject.controller;

import com.yomahub.liteflow.example.threadproject.SingletonThreadPool;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("thread")
public class ThreadDemoController {
    private static AtomicInteger counter = new AtomicInteger();

    @GetMapping("demo")
    public void demo(){
        SingletonThreadPool executor = SingletonThreadPool.getInstance();

        CountDownLatch latch = new CountDownLatch(15);


        if (!executor.isShutdown()) {
            for (int j = 0; j < 5; j++) {
                executor.execute(new MyTask(latch));
            }
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        executor.shutdown();
        counter.incrementAndGet();
        System.out.println("所有线程执行完毕，并发请求的次数， 计数器值为：" + counter.get());
        executor.requestReset();
    }

}
